package hnevc.com.netfoodchoosez;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.Serializable;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class SettingActivity extends AppCompatActivity {

    private static final String TAG = "SettingActivity";
    EditText mEtUserName;
    RadioGroup mRgGender;
    CheckBox mCbFish,mCbHot,mCbSour;
    SeekBar mSbBudget;//预算
    Button mBtnFind;//查找按钮
    String gender;
    int budget;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        initEvent();
    }

    private void initView() {
        mEtUserName = (EditText) findViewById(R.id.id_et_username);
        mRgGender = (RadioGroup) findViewById(R.id.id_rg_sex);
        mSbBudget = (SeekBar) findViewById(R.id.id_sk_budget);
        mCbFish = (CheckBox) findViewById(R.id.id_cb_fish);
        mCbHot = (CheckBox) findViewById(R.id.id_cb_hot);
        mCbSour = (CheckBox) findViewById(R.id.id_cb_sour);
        mBtnFind = (Button) findViewById(R.id.id_btn_findfood);
    }
    private void initEvent() {
        mRgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if(checkedId==R.id.id_rb_male)
                    gender = "男";
                else
                    gender ="女";
            }
        });
        mBtnFind.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
            //TODO 获取设置数据，并将数据传递给ShowFoodActivity
                //获取数据
                final Food food = new Food();
                food.setFoodSour(mCbSour.isChecked()?1:0);
                food.setFoodHot(mCbHot.isChecked()?1:0);
                food.setFoodFish(mCbFish.isChecked()?1:0);
                food.setFoodPrice(budget);

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //构造json
                        JSONObject object = new JSONObject();
                        try {
                            object.put("foodHot",food.getFoodHot());
                            object.put("foodSour",food.getFoodSour());
                            object.put("foodFish",food.getFoodFish());
                            object.put("foodPrice",food.getFoodPrice());
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String data = object.toString();//data就是需要上传得数据

                        //创建一个支持Cookie的client
                        OkHttpClient client = new OkHttpClient.Builder()
                                .cookieJar(new MyCookieJar())
                                .build();
                        //准备提交的RequestBody
                        RequestBody body = RequestBody.create(MediaType.parse("application/json"),data);

                        //构建Request对象
                        String urls =
                                "http://"+getResources().getString(R.string.ServerIp)+":8080/RestaurantServer/Food/doFindFoodsJson";
                        Request request = new Request.Builder()
                                .url(urls)
                                .post(body)
                                .build();

                        //执行请求
                        try {
                            Response response = client.newCall(request).execute();
                            String s =response.body().string();
                            //Gson解析
                            Gson gson= new Gson();
                            final List<Food> foodList =
                                    gson.fromJson(s,new TypeToken<List<Food>>(){}.getType());
                            //跳转到显示页面
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Intent intent = new Intent();
                                    intent.setClass(SettingActivity.this,ShowFoodActivity.class);
                                    intent.putExtra("foodList", (Serializable) foodList);
                                    startActivity(intent);

                                }
                            });


                            Log.e(TAG,"run:"+s);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();




            }
        });
        mSbBudget.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(fromUser){
                    budget =(int) progress;
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }
}
